<template>
  <div class="Login">
   <div class="box">
      <h2 class="text">登录</h2>
      <van-cell-group>
      <van-field
        v-model="user.username"
        required
        label="用户名"
        placeholder="请输入用户名"
      />
      <van-field v-model="user.pwd" placeholder="请输入密码" required label="密码" type="password" />
      <van-button type="info" @click="login">登录</van-button>
      <p class="register" @click="$router.replace('/register')">注册</p>
    </van-cell-group>
   </div>
  </div>
</template>

<script>
import { userLogin } from '../../api'
export default {
  data () {
    return {
      user: {
        username: '',
        pwd: ''
      }
    }
  },
  methods: {
    login () {
      userLogin({
        username: this.user.username,
        pwd: this.user.pwd,
        deviceId: 111,
        deviceName: 'dfertyu'
      }).then(res => {
        // 缓存token
        localStorage.setItem('token', res.data.data.token)
        // 缓存用户id
        localStorage.setItem('uid', res.data.data.uid)
        if (res.data.code === 0) {
          this.$toast({
            message: '登录成功',
            duration: 1000,
            onClose: () => {
              // 去主页
              this.$router.replace('/home')
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.Login{
  width: 100%;
  height: 70vh;
  display: flex;
  align-items: center;
  .box{
    flex: 1;
    .text{
      display: flex;
      justify-content: center;
      align-items: center;
      padding-bottom: 50px;
      font-size: 30px;
    }
    .van-button{
      margin-top: 30px;
      width: 100%;
    }
    .register{
      margin-top: 10px;
      color: blue;
      font-size: 14px;
    }
  }
}

</style>
